/**
 * 时间处理工具
 */
export default class DateTimeUtil {

    public static readonly ONE_DAY_TS:number = 86400;

    constructor() {
    }
    
    /**
     * 将秒数转换成 时:分:秒 形式
     * @param second 秒数
     */
    public static formatSecondToHHMMSS(second:number):string {
        let hour:number = 0;
        let minute:number = 0;
        if (second >= 3600) {
            hour = Math.floor(second/3600);
            second -= (hour * 3600);
        }
        if (second >= 60) {
            minute = Math.floor(second/60);
            second -= (minute * 60);
        }
        let showHour:string = "";
        if (hour < 10) {
            showHour = "0" + hour;
        }
        else {
            showHour = hour.toString();
        }
        
        let showMinute:string = "";
        if (minute < 10) {
            showMinute = "0" + minute;
        }
        else {
            showMinute = minute.toString();
        }
        
        let showSecond:string = "";
        if (second < 10) {
            showSecond = "0" + second;
        }
        else {
            showSecond = second.toString();
        }
        return `${showHour}:${showMinute}:${showSecond}`;


    }
    
    /**
     * 将秒数转换成 时:分:秒 形式(中文模式)
     * @param second 秒数
     */
    public static formatSecondToChineseHHMMSS(second:number) {
        let hour:number = 0;
        let minute:number = 0;
        if (second >= 3600) {
            hour = Math.floor(second/3600);
            second -= (hour * 3600);
        }
        if (second >= 60) {
            minute = Math.floor(second/60);
            second -= (minute * 60);
        }
        let showHour:string = "";
        if (hour <= 0) {
            showHour = "";
        }
        else {
            showHour = hour.toString() + "小时";
        }
        
        let showMinute:string = "";
        if (minute <= 0) {
            showMinute = "" ;
        }
        else {
            showMinute = minute.toString() + "分";
        }
        
        let showSecond:string = "";
        if (second  <= 0) {
            showSecond = "";
        }
        else {
            showSecond = second.toString() + "秒";
        }
        return `${showHour}${showMinute}${showSecond}`;
    }

    public static nowTs():number {
        return this.date2ts(new Date());
    }

    public static date2ts(date:Date):number {
        return Math.round(date.getTime() / 1000);
    }

    public static midNightTs():number {
        let date:Date = new Date();
        date.setHours(0, 0, 0, 0);
        return this.date2ts(date);
    }

    public static isSameDay(ts:number):Boolean {
        let now:number = this.nowTs();
        return Math.round(now / this.ONE_DAY_TS) == Math.round(ts / this.ONE_DAY_TS);
    }
}
